import os
import cv2

# 输入和输出目录
input_dir = "/media/cdatc/123/yzc_share/HX/co/images/train_oral"
output_dir = "/media/cdatc/123/yzc_share/HX/co/images/train"

# 确保输出目录存在
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

# 最长边缩放到640的函数
def resize_image(image_path, output_path):
    img = cv2.imread(image_path)
    h, w = img.shape[:2]
    max_dim = 640
    if h > w:
        new_h = max_dim
        new_w = int(w * (max_dim / h))
    else:
        new_w = max_dim
        new_h = int(h * (max_dim / w))
    resized_img = cv2.resize(img, (new_w, new_h))
    cv2.imwrite(output_path, resized_img)

# 遍历输入目录中的图像文件
for filename in os.listdir(input_dir):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        input_path = os.path.join(input_dir, filename)
        output_path = os.path.join(output_dir, filename)
        resize_image(input_path, output_path)

print("图像缩放完成并保存到目录:", output_dir)
